import { createSlice } from "@reduxjs/toolkit";
import { addToken, setTokenLocal, http } from "@/utils";
const loginStore = createSlice({
  name: "login",
  initialState: {
    token: addToken() || "",
    path: "",
  },
  reducers: {
    setToken(state, action) {
      state.token = action.payload;
    },
    delToken(state) {
      state.token = "";
    },
    setLocation(state, action) {
      state.path = action.payload;
    },
  },
});
const getToken = (formData) => {
  return async (dispatch) => {
    const { data } = await http.post("/authorizations", formData);
    // console.log(data);
    if (data.message === "OK") {
      dispatch(setToken(data.data.token));
      setTokenLocal(data.data.token);
    }
  };
};
const { setToken, delToken, setLocation } = loginStore.actions;
export { setToken, delToken, getToken, setLocation };
const reducer = loginStore.reducer;
export default reducer;
